﻿ 
using Newtonsoft.Json;
using System.IO;
using System.Threading.Tasks;

namespace Meiam.System.Common
{
    public static class FileConfig
    {
        private static string filePath = "File/Mail.txt";

        private static string oaFilePath = "File/OA.txt";
        private static string rabbitFilePath = "File/rabbitmq.json";

        private static MailEntity mailData = null;
        private static OAEntity oaData = null;

        public static OAEntity GetOAInfo()
        {
            if (oaData == null)
            {
                if (!File.Exists(oaFilePath)) return new OAEntity();
                var oa = File.ReadAllText(oaFilePath);
                oaData = JsonConvert.DeserializeObject<OAEntity>(oa);
            }
            //深度复制，调用方修改。
            return JsonConvert.DeserializeObject<OAEntity>(JsonConvert.SerializeObject(oaData));
        }
        public static MailEntity GetMailInfo()
        {
            if (mailData == null)
            {
                if (!File.Exists(filePath)) return new MailEntity();
                var mail =  File.ReadAllText(filePath);
                mailData = JsonConvert.DeserializeObject<MailEntity>(mail);
            }
            //深度复制，调用方修改。
            return JsonConvert.DeserializeObject<MailEntity>(JsonConvert.SerializeObject(mailData));
        }
        public static async Task<MailEntity> GetMailInfoAsync()
        {
            if (mailData == null)
            {
                if (!File.Exists(filePath)) return new MailEntity();
                var mail = await File.ReadAllTextAsync(filePath);
                mailData = JsonConvert.DeserializeObject<MailEntity>(mail);
            }
            //深度复制，调用方修改。
            return JsonConvert.DeserializeObject<MailEntity>(JsonConvert.SerializeObject(mailData));
        }

        public static async Task<bool> SaveMailInfoAsync(MailEntity mailEntity)
        {
            mailData = mailEntity;
            await File.WriteAllTextAsync(filePath, JsonConvert.SerializeObject(mailEntity));
            return true;
        }
    }
}
